var express = require("express");
var router = express.Router();
var db = require("../../model/db");

//获取购物车数据
router.get("/cart", async (req, res) => {
  const carts = await db.query("SELECT * FROM eyeglasses_cart");
  res.send({
    code: 0,
    msg: "获取数据成功",
    carts
  });
});

//修改购物车数据状态
router.put('/upcart', async (req, res) => {
  let { id, status } = req.body
  await db.query(`update eyeglasses_cart set status = ${status} where id = ${id}`)
})

//全选
router.put('/upcartall', async (req, res) => {
  let { id, status } = req.body
  await db.query(`update eyeglasses_cart set status = ${status} where id in (${id.join()})`)
})

//清空
router.delete('/delcartall', async (req, res) => {
  await db.query('truncate table eyeglasses_cart')
})

//商品数量加/减
router.put('/cartlen', async (req, res) => {
  let { id, count } = req.body
  await db.query(`update eyeglasses_cart set count = ${count} where id = ${id}`)
})

//首页数据渲染筛选
router.get("/shop", async (req, res, next) => {
  let id = req.query.id
  console.log(id);
  const data = await db.query("select * from eyeglasses_shop");
  if (id) {
    //从数据库中找到对应id的数据
    const [data] = await db.query("select * from eyeglasses_shop where id=?", [id]);
    console.log(data);
    res.send({
      msg: "获取数据成功",
      data
    })
  } else {
    res.send({
      msg: "获取数据成功",
      data
    });
  }

});
module.exports = router;
